// MapEditorView.cpp : implementation of the CMapEditorView class
//

#include "stdafx.h"
#include "MapEditor.h"
#include "MapEditorView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


IMPLEMENT_DYNCREATE(CMapEditorView, CScrollView)

BEGIN_MESSAGE_MAP(CMapEditorView, CScrollView)
	ON_WM_ERASEBKGND()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONUP()
END_MESSAGE_MAP()


CMapEditorView::CMapEditorView()
{}

CMapEditorView::~CMapEditorView()
{}

BOOL CMapEditorView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CScrollView::PreCreateWindow(cs);
}
void CMapEditorView::DocToClient(CRect& rect)
{
	CClientDC dc(this);
	OnPrepareDC(&dc, NULL);
	dc.LPtoDP(rect);
	rect.NormalizeRect();
}

void CMapEditorView::OnDraw(CDC* pDC)
{
	CMapEditorDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CDC dc;
	CDC* pDrawDC = pDC;
	CBitmap bitmap;
	CBitmap* pOldBitmap = 0;

	CRect client;
	pDC->GetClipBox(client);
	CRect rect = client;
	DocToClient(rect);

	if (!pDC->IsPrinting())
	{
		if (dc.CreateCompatibleDC(pDC))
		{
			if (bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()))
			{
				OnPrepareDC(&dc, NULL);
				pDrawDC = &dc;

				dc.OffsetViewportOrg(-rect.left, -rect.top);
				pOldBitmap = dc.SelectObject(&bitmap);
				dc.SetBrushOrg(rect.left % 8, rect.top % 8);
				dc.IntersectClipRect(client);
			}
		}
	}

	CBrush brush;
	if (!brush.CreateSolidBrush(RGB(239, 247, 238)))
		return;

	brush.UnrealizeObject();
	pDrawDC->FillRect(client, &brush);

	if (!pDC->IsPrinting())
	{
		m_GameMap.OnDraw(pDrawDC, client);
	}

	if (pDrawDC != pDC)
	{
		pDC->SetViewportOrg(0, 0);
		pDC->SetWindowOrg(0,0);
		pDC->SetMapMode(MM_TEXT);
		dc.SetViewportOrg(0, 0);
		dc.SetWindowOrg(0,0);
		dc.SetMapMode(MM_TEXT);
		pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &dc, 0, 0, SRCCOPY);
		dc.SelectObject(pOldBitmap);
	}
}

void CMapEditorView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
	
	CMapEditorDoc* doc = GetDocument();
	ASSERT(doc);
	CDC* dc = GetDC();
	ASSERT(dc);
	SetScrollSizes(MM_TEXT, m_GameMap.GetTotalSize(dc));
}

#ifdef _DEBUG
void CMapEditorView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CMapEditorView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CMapEditorDoc* CMapEditorView::GetDocument() const
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMapEditorDoc)));
	return (CMapEditorDoc*)m_pDocument;
}
#endif

BOOL CMapEditorView::OnEraseBkgnd(CDC* pDC)
{
	return TRUE;
}
void CMapEditorView::UpdateMapGridCount()
{
	OnInitialUpdate();
}
void CMapEditorView::OnMDIActivate(BOOL bActivate)
{
	m_GameMap.OnMDIActivate(bActivate);
}
LRESULT CMapEditorView::OnGridItemValueChanged(CXTPPropertyGridItem* item)
{
	LRESULT ret = m_GameMap.OnGridItemValueChanged(item);
	UpdateMapGridCount();
	return ret;
}
void CMapEditorView::OnTaskPanelItemClick(EGRIDINFO idx)
{
	m_GameMap.OnTaskPanelItemClick(idx);
}
void CMapEditorView::Serialize(CArchive& ar)
{
	m_GameMap.Serialize(ar);
}
void CMapEditorView::OnMouseMove(UINT nFlags, CPoint point)
{	
	CScrollView::OnMouseMove(nFlags, point);

	CRect client_rc(0);
	GetClientRect(&client_rc);
	
	CDC* dc = GetDC();
	if (NULL == dc)
		return;

	CSize title_size = m_GameMap.get_title_size(dc);
	CRect map_rc = m_GameMap.GetMapRect(client_rc, title_size);

	m_GameMap.OnMouseMove(map_rc, nFlags, point);

	Invalidate(TRUE);
	UpdateWindow();
}

void CMapEditorView::OnLButtonUp(UINT nFlags, CPoint point)
{

	CScrollView::OnLButtonUp(nFlags, point);

	CRect client_rc(0);
	GetClientRect(&client_rc);

	CDC* dc = GetDC();
	if (NULL == dc)
		return;

	CSize title_size = m_GameMap.get_title_size(dc);
	CRect map_rc = m_GameMap.GetMapRect(client_rc, title_size);

	if (PtInRect(&map_rc, point))
	{
		m_GameMap.OnLButtonUp(map_rc, nFlags, point);
	}
	
	Invalidate(TRUE);
	UpdateWindow();
}

void CMapEditorView::OnRButtonUp(UINT nFlags, CPoint point)
{
	CScrollView::OnRButtonUp(nFlags, point);

	CRect client_rc(0);
	GetClientRect(&client_rc);
	CDC* dc = GetDC();
	if (NULL == dc)
		return;

	CSize title_size = m_GameMap.get_title_size(dc);
	CRect map_rc = m_GameMap.GetMapRect(client_rc, title_size);

	if (PtInRect(&map_rc, point))
	{
		m_GameMap.OnRButtonUp(map_rc, nFlags, point);
	}

	Invalidate(TRUE);
	UpdateWindow();
}
